package cn.wen.javax.servlet.http;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

public class MimeMapping {

    public static final Map<String, String> DEFAULT_CONTENTTYPES = new HashMap<String, String>(101);

    private static Map<String, String> contentTypes;

    public static final String getContentType(String filename) {
	String extn = FilenameUtils.getExtension(filename);
	if (!StringUtils.isEmpty(extn)) {
	    return contentTypes.get(extn.toLowerCase());
	}
	return contentTypes.get(filename.toLowerCase());
    }

    public static final void addContentType(String extn, String type) {
	contentTypes.put(extn, type.toLowerCase());
    }

    public static final void removeContentType(String extn) {
	contentTypes.remove(extn.toLowerCase());
    }

    public static final Set<String> getExtensions() {
	return contentTypes.keySet();
    }

    static {
	DEFAULT_CONTENTTYPES.put("txt", "text/plain");
	DEFAULT_CONTENTTYPES.put("json", "application/json");
	DEFAULT_CONTENTTYPES.put("html", "text/html");
	DEFAULT_CONTENTTYPES.put("htm", "text/html");
	DEFAULT_CONTENTTYPES.put("gif", "image/gif");
	DEFAULT_CONTENTTYPES.put("jpg", "image/jpeg");
	DEFAULT_CONTENTTYPES.put("jpe", "image/jpeg");
	DEFAULT_CONTENTTYPES.put("jpeg", "image/jpeg");
	DEFAULT_CONTENTTYPES.put("png", "image/png");
	DEFAULT_CONTENTTYPES.put("java", "text/plain");
	DEFAULT_CONTENTTYPES.put("body", "text/html");
	DEFAULT_CONTENTTYPES.put("rtx", "text/richtext");
	DEFAULT_CONTENTTYPES.put("tsv", "text/tab-separated-values");
	DEFAULT_CONTENTTYPES.put("etx", "text/x-setext");
	DEFAULT_CONTENTTYPES.put("ps", "application/x-postscript");
	DEFAULT_CONTENTTYPES.put("class", "application/java");
	DEFAULT_CONTENTTYPES.put("csh", "application/x-csh");
	DEFAULT_CONTENTTYPES.put("sh", "application/x-sh");
	DEFAULT_CONTENTTYPES.put("tcl", "application/x-tcl");
	DEFAULT_CONTENTTYPES.put("tex", "application/x-tex");
	DEFAULT_CONTENTTYPES.put("texinfo", "application/x-texinfo");
	DEFAULT_CONTENTTYPES.put("texi", "application/x-texinfo");
	DEFAULT_CONTENTTYPES.put("t", "application/x-troff");
	DEFAULT_CONTENTTYPES.put("tr", "application/x-troff");
	DEFAULT_CONTENTTYPES.put("roff", "application/x-troff");
	DEFAULT_CONTENTTYPES.put("man", "application/x-troff-man");
	DEFAULT_CONTENTTYPES.put("me", "application/x-troff-me");
	DEFAULT_CONTENTTYPES.put("ms", "application/x-wais-source");
	DEFAULT_CONTENTTYPES.put("src", "application/x-wais-source");
	DEFAULT_CONTENTTYPES.put("zip", "application/zip");
	DEFAULT_CONTENTTYPES.put("bcpio", "application/x-bcpio");
	DEFAULT_CONTENTTYPES.put("cpio", "application/x-cpio");
	DEFAULT_CONTENTTYPES.put("gtar", "application/x-gtar");
	DEFAULT_CONTENTTYPES.put("shar", "application/x-shar");
	DEFAULT_CONTENTTYPES.put("sv4cpio", "application/x-sv4cpio");
	DEFAULT_CONTENTTYPES.put("sv4crc", "application/x-sv4crc");
	DEFAULT_CONTENTTYPES.put("tar", "application/x-tar");
	DEFAULT_CONTENTTYPES.put("ustar", "application/x-ustar");
	DEFAULT_CONTENTTYPES.put("dvi", "application/x-dvi");
	DEFAULT_CONTENTTYPES.put("hdf", "application/x-hdf");
	DEFAULT_CONTENTTYPES.put("latex", "application/x-latex");
	DEFAULT_CONTENTTYPES.put("bin", "application/octet-stream");
	DEFAULT_CONTENTTYPES.put("oda", "application/oda");
	DEFAULT_CONTENTTYPES.put("pdf", "application/pdf");
	DEFAULT_CONTENTTYPES.put("ps", "application/postscript");
	DEFAULT_CONTENTTYPES.put("eps", "application/postscript");
	DEFAULT_CONTENTTYPES.put("ai", "application/postscript");
	DEFAULT_CONTENTTYPES.put("rtf", "application/rtf");
	DEFAULT_CONTENTTYPES.put("nc", "application/x-netcdf");
	DEFAULT_CONTENTTYPES.put("cdf", "application/x-netcdf");
	DEFAULT_CONTENTTYPES.put("cer", "application/x-x509-ca-cert");
	DEFAULT_CONTENTTYPES.put("exe", "application/octet-stream");
	DEFAULT_CONTENTTYPES.put("gz", "application/x-gzip");
	DEFAULT_CONTENTTYPES.put("Z", "application/x-compress");
	DEFAULT_CONTENTTYPES.put("z", "application/x-compress");
	DEFAULT_CONTENTTYPES.put("hqx", "application/mac-binhex40");
	DEFAULT_CONTENTTYPES.put("mif", "application/x-mif");
	DEFAULT_CONTENTTYPES.put("ief", "image/ief");
	DEFAULT_CONTENTTYPES.put("tiff", "image/tiff");
	DEFAULT_CONTENTTYPES.put("tif", "image/tiff");
	DEFAULT_CONTENTTYPES.put("ras", "image/x-cmu-raster");
	DEFAULT_CONTENTTYPES.put("pnm", "image/x-portable-anymap");
	DEFAULT_CONTENTTYPES.put("pbm", "image/x-portable-bitmap");
	DEFAULT_CONTENTTYPES.put("pgm", "image/x-portable-graymap");
	DEFAULT_CONTENTTYPES.put("ppm", "image/x-portable-pixmap");
	DEFAULT_CONTENTTYPES.put("rgb", "image/x-rgb");
	DEFAULT_CONTENTTYPES.put("xbm", "image/x-xbitmap");
	DEFAULT_CONTENTTYPES.put("xpm", "image/x-xpixmap");
	DEFAULT_CONTENTTYPES.put("xwd", "image/x-xwindowdump");
	DEFAULT_CONTENTTYPES.put("au", "audio/basic");
	DEFAULT_CONTENTTYPES.put("snd", "audio/basic");
	DEFAULT_CONTENTTYPES.put("aif", "audio/x-aiff");
	DEFAULT_CONTENTTYPES.put("aiff", "audio/x-aiff");
	DEFAULT_CONTENTTYPES.put("aifc", "audio/x-aiff");
	DEFAULT_CONTENTTYPES.put("wav", "audio/x-wav");
	DEFAULT_CONTENTTYPES.put("mpeg", "video/mpeg");
	DEFAULT_CONTENTTYPES.put("mpg", "video/mpeg");
	DEFAULT_CONTENTTYPES.put("mpe", "video/mpeg");
	DEFAULT_CONTENTTYPES.put("qt", "video/quicktime");
	DEFAULT_CONTENTTYPES.put("mov", "video/quicktime");
	DEFAULT_CONTENTTYPES.put("avi", "video/x-msvideo");
	DEFAULT_CONTENTTYPES.put("movie", "video/x-sgi-movie");
	DEFAULT_CONTENTTYPES.put("avx", "video/x-rad-screenplay");
	DEFAULT_CONTENTTYPES.put("wrl", "x-world/x-vrml");
	DEFAULT_CONTENTTYPES.put("mpv2", "video/mpeg2");

	DEFAULT_CONTENTTYPES.put("xml", "text/xml");
	DEFAULT_CONTENTTYPES.put("xsl", "text/xml");
	DEFAULT_CONTENTTYPES.put("svg", "image/svg+xml");
	DEFAULT_CONTENTTYPES.put("svgz", "image/svg+xml");
	DEFAULT_CONTENTTYPES.put("wbmp", "image/vnd.wap.wbmp");
	DEFAULT_CONTENTTYPES.put("wml", "text/vnd.wap.wml");
	DEFAULT_CONTENTTYPES.put("wmlc", "application/vnd.wap.wmlc");
	DEFAULT_CONTENTTYPES.put("wmls", "text/vnd.wap.wmlscript");
	DEFAULT_CONTENTTYPES.put("wmlscriptc", "application/vnd.wap.wmlscriptc");

	DEFAULT_CONTENTTYPES.put("rptdesign", "application/x-download");

	DEFAULT_CONTENTTYPES.put("xls", "application/vnd.ms-excel");

	contentTypes = new HashMap<String, String>(DEFAULT_CONTENTTYPES);
    }

}
